<template>
  <el-dialog
    :model-value="visible"
    @update:model-value="$emit('update:visible', $event)"
    title="预约详情"
    width="700px"
    :show-close="true"
  >
    <div class="booking-detail-dialog" v-if="bookingDetail">
      <div class="detail-section">
        <h3>预约信息</h3>
        <div class="detail-grid">
          <div class="detail-item">
            <label>预约编号：</label>
            <span class="booking-number">{{ bookingDetail.bookingNumber }}</span>
          </div>
          <div class="detail-item">
            <label>预约状态：</label>
            <el-tag :type="getStatusTagType(bookingDetail.status)">
              {{ getStatusText(bookingDetail.status) }}
            </el-tag>
          </div>
          <div class="detail-item">
            <label>预约日期：</label>
            <span>{{ formatDate(bookingDetail.bookingDate) }}</span>
          </div>
          <div class="detail-item">
            <label>预约场次：</label>
            <el-tag :type="getPeriodTagType(bookingDetail.periodName)">
              {{ bookingDetail.periodName }}
            </el-tag>
          </div>
          <div class="detail-item">
            <label>预约时间：</label>
            <span>{{ bookingDetail.bookingTimeStart }} - {{ bookingDetail.bookingTimeEnd }}</span>
          </div>
        </div>
      </div>
      
      <div class="detail-section">
        <h3>联系人信息</h3>
        <div class="detail-grid">
          <div class="detail-item">
            <label>联系人姓名：</label>
            <span>{{ bookingDetail.contactName || '未填写' }}</span>
          </div>
          <div class="detail-item">
            <label>联系电话：</label>
            <span>{{ bookingDetail.contactPhone || '未填写' }}</span>
          </div>
        </div>
      </div>
      
      <div class="detail-section">
        <h3>详细个人信息</h3>
        <div class="detail-grid">
          <div class="detail-item">
            <label>性别：</label>
            <span>{{ bookingDetail.gender || '未填写' }}</span>
          </div>
          <div class="detail-item">
            <label>年龄：</label>
            <span>{{ bookingDetail.age ? bookingDetail.age + '岁' : '未填写' }}</span>
          </div>
          <div class="detail-item">
            <label>教育程度：</label>
            <span>{{ bookingDetail.education || '未填写' }}</span>
          </div>
          <div class="detail-item">
            <label>职业：</label>
            <span>{{ bookingDetail.occupation || '未填写' }}</span>
          </div>
          <div class="detail-item">
            <label>宗教信仰：</label>
            <span>{{ bookingDetail.religion || '未填写' }}</span>
          </div>
          <div class="detail-item">
            <label>掌握语种：</label>
            <span>{{ formatLanguages(bookingDetail.languages) }}</span>
          </div>
        </div>
      </div>
      

      
      <div class="detail-section">
        <h3>时间记录</h3>
        <div class="detail-grid">
          <div class="detail-item">
            <label>预约时间：</label>
            <span>{{ formatDateTime(bookingDetail.createdAt) }}</span>
          </div>
          <div class="detail-item">
            <label>更新时间：</label>
            <span>{{ formatDateTime(bookingDetail.updatedAt) }}</span>
          </div>
          <div class="detail-item" v-if="bookingDetail.completedAt">
            <label>完成时间：</label>
            <span>{{ formatDateTime(bookingDetail.completedAt) }}</span>
          </div>
        </div>
      </div>
    </div>
    
    <template #footer>
      <div class="dialog-footer">
        <button class="cancel-btn" @click="$emit('update:visible', false)">
          <span class="btn-content">
            关闭
          </span>
        </button>
      </div>
    </template>
  </el-dialog>
</template>

<script setup>
defineProps({
  visible: {
    type: Boolean,
    default: false
  },
  bookingDetail: {
    type: Object,
    default: () => null
  }
})

defineEmits(['update:visible'])

// 格式化函数
const formatDate = (dateStr) => {
  if (!dateStr) return '未知'
  const date = new Date(dateStr)
  return date.toLocaleDateString('zh-CN')
}

const formatDateTime = (dateTimeStr) => {
  if (!dateTimeStr) return '未知'
  
  try {
    const date = new Date(dateTimeStr)
    return date.toLocaleString('zh-CN', {
      year: 'numeric',
      month: '2-digit',
      day: '2-digit',
      hour: '2-digit',
      minute: '2-digit',
      second: '2-digit'
    })
  } catch (error) {
    return '格式错误'
  }
}

// 格式化语种显示
const formatLanguages = (languages) => {
  if (!languages) return '未填写'
  
  try {
    // 如果是JSON字符串，尝试解析
    if (typeof languages === 'string' && languages.startsWith('[')) {
      const languageArray = JSON.parse(languages)
      return Array.isArray(languageArray) ? languageArray.join('、') : languages
    }
    return languages
  } catch (error) {
    // 解析失败，直接返回原字符串
    return languages
  }
}

const getStatusText = (status) => {
  const statusMap = {
    '已预约': '已预约',
    '已取消': '已取消',
    '已完成': '已完成',
    'confirmed': '已确认',
    'completed': '已完成',
    'no_show': '未到场'
  }
  return statusMap[status] || status
}

const getStatusTagType = (status) => {
  const statusTypeMap = {
    '已预约': 'primary',
    '已取消': 'danger',
    '已完成': 'success',
    'confirmed': 'primary',
    'completed': 'success',
    'no_show': 'danger'
  }
  return statusTypeMap[status] || 'info'
}

const getPeriodTagType = (period) => {
  const periodTypeMap = {
    '上午': 'success',
    '下午': 'warning',
    '晚上': 'info',
    '上午场次': 'success',
    '下午场次': 'warning',
    '晚上场次': 'info'
  }
  return periodTypeMap[period] || 'info'
}
</script>

<style scoped>
/* 预约详情弹窗 */
.booking-detail-dialog {
  padding: 0;
}

.detail-section {
  margin-bottom: 25px;
  padding-bottom: 20px;
  border-bottom: 1px solid #f0f0f0;
}

.detail-section:last-child {
  border-bottom: none;
  margin-bottom: 0;
  padding-bottom: 0;
}

.detail-section h3 {
  margin: 0 0 15px 0;
  font-size: 18px;
  font-weight: bold;
  color: #333;
}

.detail-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(280px, 1fr));
  gap: 15px;
}

.detail-item {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 10px 0;
}

.detail-item label {
  font-weight: 600;
  color: #666;
  margin-right: 10px;
  white-space: nowrap;
}

.detail-item span {
  color: #333;
  text-align: right;
  flex: 1;
}

.booking-number {
  font-family: 'Courier New', monospace;
  font-weight: bold;
  color: #667eea;
}

.dialog-footer {
  display: flex;
  justify-content: flex-end;
  gap: 15px;
  margin-top: 25px;
  padding-top: 20px;
  border-top: 1px solid #f0f0f0;
}

.cancel-btn {
  padding: 12px 20px;
  background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
  color: #666;
  border: 1px solid #e5e7eb;
  border-radius: 8px;
  font-size: 1rem;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.3s ease;
  min-height: 44px;
  display: flex;
  align-items: center;
  justify-content: center;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
  min-width: 120px;
}

.cancel-btn:hover {
  background: linear-gradient(135deg, #e5e7eb 0%, #d1d5db 100%);
  transform: translateY(-1px);
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
}

.cancel-btn:active {
  transform: translateY(0);
}

.btn-content {
  display: flex;
  align-items: center;
  gap: 8px;
}
</style> 